package com.service.impl;

import com.bean.Power;
import com.bean.Role;
import com.bean.User;
import com.dao.PowerDao;
import com.dao.RoleDao;
import com.dao.UserDao;
import com.dao.impl.PowerDaoImpl;
import com.dao.impl.RoleDaoImpl;
import com.dao.impl.UserDaoImpl;
import com.service.UserService;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    private RoleDao roleDao =new RoleDaoImpl();
    private PowerDao powerDao = new PowerDaoImpl();

    @Override
    public List<User> getAllUsers() {
        List<User> userList  = new ArrayList<>();
        userList = userDao.getAllUsers();
        for (User user:userList) {
            List<Role> roleList = new ArrayList<>();
            roleList = roleDao.getRoles(user);
            user.setRoleList(roleList);
        }
        return userList;
    }

    @Override
    public User returnUser(String account) {
        User user = userDao.returnUser(account);
        List<Role> roleList = new ArrayList<>();
        roleList = roleDao.getRoles(user);
        user.setRoleList(roleList);
        for (Role role:roleList) {
            List<Power> powerList = new ArrayList<>();
            powerList = powerDao.getPower(role);
            role.setPowerList(powerList);
        }
        return user;
    }

    @Override
    public boolean accountPasswordIsEqual(String account, String password) {
        if(userDao.accountPasswordIsEqual(account,password) > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<String> getNowUserPower(User user) {
        List<String> powerList = new ArrayList<>();
        List<Role> roleList = new ArrayList<>();
        roleList = user.getRoleList();
        for (Role role:roleList) {
            List<Power> powerList1 = new ArrayList<>();
            powerList1 = role.getPowerList();
            for (Power power:powerList1) {
                String powerName = power.getPowerName();
                powerList.add(powerName);
            }
        }
        return powerList;
    }

    @Override
    public boolean addUser(User user, List<String> roleNames) {
        if(userDao.addUser(user) > 0){
            String account = user.getAccount();
            userDao.addUser(account,roleNames);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteUser(int key) {
        if(userDao.deleteUser(key) > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateUserRole(int key, List<String> roleNames) {
        if (userDao.updateUserRole(key, roleNames) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int updatePassword(User user, String oldPsw, String NewPsw, String sedNewPsw) {
        int m = -1;
        if (!user.getPassword().equals(oldPsw)){
            m = 1;
        }else {
            if(!NewPsw.equals(sedNewPsw)){
                m = 2;
            }else if(NewPsw.equals(sedNewPsw)){
                if(userDao.updatePassword(user,NewPsw) > 0){
                    m = 3;
                }else {
                    m = 4;
                }

            }
        }
        return m;
    }

    @Override
    public List<User> getFindUsers(String userName) {
        return userDao.getFindUsers(userName);
    }

    @Override
    public User getSelectUser(int key) {
        return userDao.getSelectUser(key);
    }
}
